package com.jye.rseven.user.serivce.impl;

import org.springframework.stereotype.Service;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Credit;
import com.jye.rseven.domain.CreditRecord;
import com.jye.rseven.domain.Task;
import com.jye.rseven.user.mapper.*;
import com.jye.rseven.service.CreditService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class CreditServiceImpl implements CreditService {


    @Autowired
    CreditMapper creditMapper;

    @Autowired
    PromotionMapper promotionMapper;

    @Autowired
    TaskMapper taskMapper;

    @Autowired
    CreditRecordMapper creditRecordMapper;

    @Override
    public int addCredit(Credit credit) {
      try{
          creditMapper.addCredit(credit);
          return 1;
      }catch (Exception e){
        System.out.println(e);
        return 0;
      }
       // return creditMapper.addCredit(credit);
    }

    /*
     * 查询积分模块
     *
     * */
    @Override
    public Credit findMyCreditByUserId(int uId) {
        return creditMapper.findMyCreditByUserId(uId);
    }

    /*
     * 查询历史积分
     *
     * */
    @Override
    public List<CreditRecord>  findMyCreditRecordByuId(int uId){
        return creditRecordMapper.findMyCreditRecordByUserId(uId);
    }

    /*
     * 查询用户上个月积分
     *
     * */
    @Override
    public CreditRecord  findMyCreditRecord(int uId){
        return creditRecordMapper.findMyCreditRecord(uId);
    }
    /*
     * 积分兑换金币
     *
     * */
    @Override
    public int updateCredit(int uId,int credit) {
        try{
            //Credit c=new Credit();
            if(credit>=10&&credit%10==0){
           // creditMapper.updateCredit(uId,credit);
            int gold=credit/10;
            String lastRemark=MyConstants.creditRemark.changeGold;
           // c.setLastRemark(lastRemark);
            creditMapper.updateCredit(uId,credit,gold,lastRemark);
          //  creditMapper.updateGold(uId,gold,lastRemark);
            return 1;
            }else{
                return 0;
            }

        }catch (Exception e){
            return 0;
        }
    }


    /*
     * 推广成功，用户获得佣金
     *
     * */
    @Override
    public int addChargeByuId(int uId) {
        try {
            String payStatus=promotionMapper.findPromotionStatusByuId(uId);
            if(payStatus== MyConstants.promotionStatus.paid){
                creditMapper.addChargeByuId(uId);
                return 1;
            }else {
                return 0;
            }
        }catch (Exception e){
            return 0;
        }
    }

    /*
     * 佣金提现
     *
     * */
    @Override
    public int updateCharge(int uId) {
        try{
            creditMapper.updateCharge(uId);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    //用户完成任务获得相应积分
    @Override
    public int addCreditByuId(int uId) {
//        try {
//                Credit cc=new Credit();
//            List<Task> myTask = taskMapper.findMyTaskByUId(uId);
//
//            int credit = 0;
//            for (Task task : myTask) {
//                //这里if里面还需判断任务的状态是否为完成，
//                // 因为这部分任务模块还没写，所以暂时没写
//                if (task.getType() == MyConstants.taskType.daily) {
//                    //每日签到得10积分
//                    credit = 10;
//                  String lastRemark=MyConstants.creditRemark.taskDaily;
//                  cc.setLastRemark(lastRemark);
//
//                } else if (task.getType() == MyConstants.taskType.activities) {
//                    //参与活动评比得40积分
//                    credit = 40;
//                 String Remark=MyConstants.creditRemark.taskActivity;
//                 cc.setLastRemark(Remark);
//                }
//                creditMapper.updateCreditByuId(uId,credit);
//                creditRecordMapper.addCreditRecordByuId(uId,credit);
//                return 1;
//            }
//        } catch (Exception e) {
//            return 0;
//        }
        return 0;
    }

    @Override
    public List<CreditRecord> findMyCreditRecordByUId(int uId) {
        return creditRecordMapper.findMyCreditRecordByUId(uId);
    }


}
